package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @文件名: CourseServiceImpl
 * @作者: DX
 * @创建日期: 2025/10/8
 * @描述:
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Override
    public List<Course> coursesBySubject(Integer subject) {
        return courseMapper.coursesBySubject(subject);
    }


    @Override
    public void update(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }


    @Override
    public Course getById(Integer id) {
        return courseMapper.getById(id);
    }


    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    @Override
    public List<Course> listAll() {
        return courseMapper.listAll();
    }

    @Override
    public void add(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.add(course);
    }

    @Override
    public PageResult<Course> pageResult(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Course> list = courseMapper.selectByPage(name, subject, target);
        Page<Course> pageInfo = (Page<Course>)list;

        return PageResult.<Course>builder()
                .total(Long.valueOf(pageInfo.getTotal()).intValue() )
                .rows(pageInfo.getResult())
                .build();
    }
}
